import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/response_data.dart';

import '../../net/api/activity_apis.dart';
import '../../net/response/activity/activity_sign_in_list_data.dart';
import '../../net/response/activity/actvity_recommend_list_data.dart';

class ActivityModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///活动列表
  Future<List<ActvityRecommendListData>> getActivityList(
      {String activityNameLike = "", int page = 1, int rows = 20}) async {
    var map = {
      "activityNameLike": activityNameLike,
      "page": page,
      "rows": rows
    };
    var data =
        await HttpGo.instance.postData(ActivityApis.activityList, data: map);
    if ("0" == data['code']) {
      var list = ((data['data']['rows'] as List) ?? [])
          .map((e) => ActvityRecommendListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///获取详情
  Future<ActvityRecommendListData> getActivityDetail(
      {String activityNo = "",String activityNameLike = "", int page = 1, int rows = 20}) async {
    var map = {"activityNo": activityNo, "page": page, "rows": rows,"activityNameLike":activityNameLike};
    var data =
        await HttpGo.instance.postData(ActivityApis.activityList, data: map);
    if ("0" == data['code']) {
      var list = ((data['data']['rows'] as List) ?? [])
          .map((e) => ActvityRecommendListData.fromJson(e))
          .toList();
      if (list.isEmpty) {
        throw "未获取到活动详情";
      } else {
        return list.firstWhere((element) => element.activityNo==activityNo);
      }
    } else {
      throw data['msg'];
    }
  }

  ///活动报名
  Future<Map> enrollActivity(
      {required String activityId,
      required String humanType,
      String joinHumanId = "",
      List<String> joinHumanIds = const [],
      String registrationRemark = "",
      String mobile = "",
      required String regClientType,
      required String activityNo,
      required String activityName}) async {
    var map = <String, dynamic>{
      "activityId": activityId,
      "humanType": humanType,
      "registrationRemark": registrationRemark,
      "regClientType": regClientType,
      "activityNo": activityNo,
      "activityName": activityName,
    };
    if (!joinHumanId.isNullOrEmpty()) {
      map['joinHumanId'] = joinHumanId;
    }

    if (!mobile.isNullOrEmpty()) {
      map['mobile'] = mobile;
    }
    if (joinHumanIds.isNotEmpty) {
      map['joinHumanIds'] = joinHumanIds;
    }

    var data =
        await HttpGo.instance.postData(ActivityApis.enrollActivity, data: map);
    var bean = ResponseData.fromJson(data);
    if ('0'== bean.code) {
      return data;
    } else {
      throw data['msg'];
    }
  }


  ///已报名老人列表
  Future<List<ActivitySignInListData>> oldSignList({required dynamic activityId,int page = 1,int rows = 20,dynamic humanid,String humanNameLikeM = ""}) async {
    var map ={
      "activityId":activityId,
      "page":page,
      "rows":rows,
      "humanNameLike":humanNameLikeM,
    };
    if(humanid!=null){
      map['joinHumanId'] = humanid;
    }
    var data =
        await HttpGo.instance.postData(ActivityApis.activityOlderList, data: map.toJsonMapStr(),options: PostRequest.urlencodedHeader);
    if ('0'== data['code']) {
      var list = ((data['data']['rows'] as List?) ?? [])
          .map((e) => ActivitySignInListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }

  }

  ///已报名志愿者列表
  Future<List<ActivitySignInListData>> volunteerSignList({required dynamic activityId,int page = 1,int rows = 20,dynamic humanid,String humanNameLikeM = ""}) async {
    var map ={
      "activityId":activityId,
      "page":page,
      "rows":rows,
      "humanNameLike":humanNameLikeM,
    };
    if(humanid!=null){
      map['joinHumanIds'] = humanid;
    }
    var data =
    await HttpGo.instance.postData(ActivityApis.activityVolunteerList, data: map.toJsonMapStr(),options: PostRequest.urlencodedHeader);
    if ('0'== data['code']) {
      var list = ((data['data']['rows'] as List) ?? [])
          .map((e) => ActivitySignInListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///签到
  Future<Map> submitSignIn(dynamic id) async {
    var map ={
      "id":id,
    };
    var data =
        await HttpGo.instance.postData(ActivityApis.submitSignIn, data: map,options: PostRequest.urlencodedHeader);
    var bean = ResponseData.fromJson(data);
    if ('0'== bean.code) {
      return data;
    } else {
      throw data['msg'];
    }
  }

}
